package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.model.product.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuImageMapper;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrValueMapper;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author dyqstart
 * @create 2021-08-05-17:09
 * @since 1.0.0
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo> implements SpuInfoService {


    @Autowired
    SpuInfoMapper spuInfoMapper;

    @Autowired
    SpuImageMapper spuImageMapper;

    @Autowired
    SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    SpuSaleAttrValueMapper spuSaleAttrValueMapper;


    /**
     * {
     *     "id": null,
     *     "spuName": "小米11",
     *     "description": "小米11小米11小米11小米11小米11",
     *     "category3Id": 61,
     *     "spuImageList": [
     *         {
     *             "imgName": "xiaomi.png",
     *             "imgUrl": "http://192.168.200.188:9000/gmall/0249699dcb2542fa95318ec4466ce627-xiaomi.png"
     *         }
     *     ],
     *     "spuSaleAttrList": [
     *         {
     *             "baseSaleAttrId": "2",
     *             "saleAttrName": "选择版本",
     *             "spuSaleAttrValueList": [
     *                 {
     *                     "baseSaleAttrId": "2",
     *                     "saleAttrValueName": "典藏版"
     *                 }
     *             ]
     *         }
     *     ],
     *     "tmId": 12
     * }
     */

    /**
     *
     * @param spuInfo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void bigSaveInfo(SpuInfo spuInfo) {

        int insert = spuInfoMapper.insert(spuInfo);
        Long spuId = spuInfo.getId();

        //1、保存 sku_image
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)){
            for (SpuImage spuImage : spuImageList) {

                spuImage.setSpuId(spuId);
                spuImageMapper.insert(spuImage);
            }
        }

        // 2、保存spu_sale_attr
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)){
            for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
                spuSaleAttr.setSpuId(spuId);
                spuSaleAttrMapper.insert(spuSaleAttr);

                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValueList) {
                    if (!CollectionUtils.isEmpty(spuSaleAttrList)){
                        spuSaleAttrValue.setSpuId(spuId);
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    }
                }
            }
        }

    }
}
